/*
 * Copyleft 2012 Power by Colen.
 *
 * Project: app-crm
 * Date: Mar 17, 2012
 */
package com.crm.common.service.pub.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.app.platform.base.utils.BeanUtil;
import com.crm.common.constant.CodeConst;
import com.crm.common.exception.CrmSysException;
import com.crm.common.service.CommonServiceEx;
import com.crm.common.service.pub.CodeDataService;

/**
 * Code Data Cache Service (Can not refresh)
 *
 * @author Colen
 */
public class CodeDataServiceImpl extends CommonServiceEx implements CodeDataService
{
    /** key split */
    private static final String KS = "-";
    
    /** Map Cache */
    private Map<String, List<Map<String, Object>>> cache = null;

    /** fast cache */
    private Map<String, Map<String, Object>> fastCache = null; 


    /**
     * initialize data from database
     *
     */
    public void initialize() {
        this.cache = new HashMap<String, List<Map<String,Object>>>();
        this.fastCache = new HashMap<String, Map<String,Object>>();

        // load code data
        List<Map<String, Object>> list = getCommonDao().namingQueryForList("sql_common_codedata_queryAll", null);

        String codeKey = null;
        List<Map<String, Object>> codeList = null;
        for (Map<String, Object> code : list) {
            codeKey = (String) code.get("code_key");
            codeList = this.cache.get(codeKey);
            if (codeList == null) {
                codeList = new ArrayList<Map<String,Object>>();
                this.cache.put(codeKey, codeList);
            }
            codeList.add(code);
            this.fastCache.put(codeKey + KS + code.get("value"), code);
        }

        // load car brand data
        list = getCommonDao().namingQueryForList("sql_common_carbrand_queryAll", null);
        codeKey = CodeConst.CDD_CARBRAND;
        this.cache.put(codeKey, list);
        for (Map<String, Object> code : list) {
            this.fastCache.put(codeKey + KS + code.get("value"), code);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Map<String, Object>> getCodeList(String codeKey) {
        List<Map<String, Object>> list = getList(codeKey);
        return (List<Map<String, Object>>) BeanUtil.cloneBean(list);
    }

    private List<Map<String, Object>> getList(String codeKey) {
        List<Map<String, Object>> list = this.cache.get(codeKey);
        if (list == null) {
            throw new CrmSysException("Can not find Code data by key (" + codeKey + ")");
        }
        return list;
    }

    /**
     * get code name by value (full name)
     *
     * @param codeKey code key
     * @param value code value
     * @return String
     */
    public String getNmByValue(String codeKey, String value) {
        return getName(codeKey, value, "full_name");
    }
    
    /**
     * get code name by value (ABBR Name)
     *
     * @param codeKey code key
     * @param value code value
     * @return String
     */
    public String getAbbrNmByValue(String codeKey, String value) {
        return getName(codeKey, value, "abbr_name");
    }
    
    private String getName(String codeKey, String value, String pKey) {
        Map<String, Object> code = this.fastCache.get(codeKey + KS + value);
        if (code == null) {
            return null;
        } else {
            return (String) code.get(pKey);
        }
    }


    @Override
    public void refresh(Map<String, Object> args) {
        // TODO More Work here.
    }

}
